<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\common\model\Authgroup as AuthgroupModel;
use app\common\model\Authrule;

class Authgroup extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $groupDatas = AuthgroupModel::paginate(2);
        return view('', compact('groupDatas'));
    }

    /**
     * 显示创建资源表单页
     *
     * @return \think\Response
     */
    public function create()
    {
        //规则数据 按级别
        $ruleDatas = Authrule::getRuleLevel();
        return view('', compact('ruleDatas'));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = input();
        if(empty($data['rules'])){
            $this->error('至少选择一个权限');
        }
        $result = $this->validate($data, 'Authgroup');
        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }
        //添加数据
        $AuthgroupM = new AuthgroupModel;
        $AuthgroupM->title = $data['title'];
        $AuthgroupM->rules = implode(',', $data['rules']);
        if ($AuthgroupM->save()) {
            $this->success('添加成功', '/admin/authgroup');
        } else {
            $this->error('操作有误');
        };
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        /*
       {if condition="in_array($ruleData['id'],explode(',',$groupData['rules']))" }
       checked
       {/if}
         */
        //用户组数据
        $groupData = AuthgroupModel::find($id);
        //规则数据
        //规则数据 按级别
        $ruleDatas = Authrule::getRuleLevel();
        return view('', compact('ruleDatas', 'groupData'));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = input();
        if(empty($data['rules'])){
            $this->error('至少选择一个权限');
        }
        $result = $this->validate($data, 'Authgroup.edit');
        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }
        $AuthgroupM = AuthgroupModel::get($id);
        $AuthgroupM->title = $data['title'];
        $AuthgroupM->rules = implode(',', $data['rules']);
        if ($AuthgroupM->save()) {
            $this->success('操作成功', '/admin/authgroup');
        } else {
            $this->error('编辑失败');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //删除数据
        if (AuthgroupModel::where('id', $id)->delete()) {
            $res['status'] = '1';
            $res['msg'] = '删除成功';
        } else {
            $res['status'] = '0';
            $res['msg'] = '删除失败';
        }
        return $res;
    }
}
